package com.story.pr;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

public class StoryAdapter extends ArrayAdapter<StoryList> {

	ArrayList<StoryList> items;
	ImageView cover;
	TextView short_title, title, description;
	RatingBar rate;

	public StoryAdapter(Context context, int textViewResourceId,
			ArrayList<StoryList> objects) {
		super(context, textViewResourceId, objects);
		this.items = objects;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View v = convertView;
		if (v == null) {
			LayoutInflater vi = (LayoutInflater) getContext().getSystemService(
					Context.LAYOUT_INFLATER_SERVICE);
			v = vi.inflate(R.layout.list, null);
		}

		StoryList storyList = items.get(position);

		if (storyList != null) {

			cover = (ImageView) v.findViewById(R.id.image_cover);
			short_title = (TextView) v.findViewById(R.id.short_title);
			title = (TextView) v.findViewById(R.id.title);
			description = (TextView) v.findViewById(R.id.description);
			rate = (RatingBar) v.findViewById(R.id.rating_bar);

			cover.setImageBitmap(GetFile(storyList.getCover()));
			short_title.setText(storyList.getShort_title());
			title.setText(storyList.getTitle());
			description.setText(storyList.getDescription());
			rate.setRating(storyList.getRate());
		}

		return v;
	}

	public void addListItem(StoryList item) {
		this.items.add(item);
	}

	// //////////////////////////////////////////////
	// Phương thức xử lý ảnh
	// ///////////////////////////////////////////////

	public static Bitmap GetFile(String sUrl) {
		InputStream in = null;
		Bitmap bitmap = null;
		HttpURLConnection cnn = null;

		try {
			in = OpenHttpConnection(sUrl);
			bitmap = BitmapFactory.decodeStream(in);
			in.close();

		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			if (cnn != null) {
				try {
					cnn.disconnect();
				} catch (Exception e) {
					// TODO: handle exception
				}
			}
		}
		return bitmap;
	}

	private static InputStream OpenHttpConnection(String urlString)
			throws IOException {
		// TODO Auto-generated method stub
		InputStream in = null;
		int response = -1;
		URL url = new URL(urlString);
		URLConnection conn = url.openConnection();

		if (!(conn instanceof HttpURLConnection))
			throw new IOException("khong phai 1 ket noi http");

		try {

			HttpURLConnection httpConn = (HttpURLConnection) conn;
			httpConn.setRequestMethod("GET");
			httpConn.connect();
			response = httpConn.getResponseCode();
			if (response == HttpURLConnection.HTTP_OK) {
				in = httpConn.getInputStream();
			}
		} catch (Exception e) {
			// TODO: handle exception
			throw new IOException("Loi ket noi");
		}

		return in;
	}
}
